import { createWebHashHistory, createRouter } from "vue-router";
import routes from "./routes";

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

// 全局路由守卫
router.beforeEach((to, from, next) => {
  // 示例：检查用户是否登录
  const userInfo = localStorage.getItem("user");
  let isLoggedIn;
  if (userInfo) {
    const data = JSON.parse(userInfo);
    isLoggedIn = data.userInfo.token;
  }
  if (to.path !== "/" && !isLoggedIn) {
    next("/"); // 未登录则跳转到登录页
  } else {
    next(); // 继续导航
  }
});

export default router;
